---@class UIExpeditionHolyStone : UIBase
UIExpeditionHolyStone = UIBase.New(PanelResNames.UIExpeditionHolyStone)
local t = UIExpeditionHolyStone

function t.Init(func, goFunc, type)
    t.goFunc = goFunc;
    t.openType = type or 1;
    UIBase.Init(t, func)
end

function t:OnCreate()
    self.costIcon = self:GetChild("icon")
    self.costNumText = self:GetChild("Number")
    self.itemList = self:GetList("HeroList")
    self.btnClose = self:GetChild("CloseBg")
    self.btnSure = self:GetChild("btnSure")
    self.btnJump = self:GetChild("btnJump")
    self.sourceUIobj = self:GetChild("UISourceMultiple");
    self.showSourceController = self:GetController("showSource")
    self.typeCtrl = self:GetController("type");
    self.txtLongJourney2 = self:GetChild("Text_LongJourney2");

    self.heroDataList = {}

    self:RegisterOnClick(self.btnClose,self.ClickClose)
    self:RegisterOnClick(self.btnSure,self.ClickSure)
    self:RegisterOnClick(self.btnJump,self.ClickGet)
    self:SetDynamicList(self.itemList,self.ItemRender)
    self:RegisterEvent(Config.EventType.Fresh_Bag,self.updateSource,self)
    self:RegisterEvent(Config.EventType.Fresh_BagUseItemBack,logicMgr.ItemManager.showGetItem)
end

function t:OnEnable()
    self:Refresh()
end
function t:OnDisable()
end
function t:OnDestroy()
end

function t:Refresh()
    self.costNumText.text = logicMgr.ItemManager.getItemCountbyModelId(EnumConst.ItemIDEnum.HolyStone)
    --self.costIcon.url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(EnumConst.ItemIDEnum.HolyStone)
    self.heroDataList = logicMgr.ExpeditionMgr.GetNeedRecoverHero()
    self.itemList.numItems = #self.heroDataList
    if self.openType == 1 then
        self.typeCtrl.selectedIndex = 1;
    elseif self.openType == 2 then
        self.typeCtrl.selectedIndex = 0;
        self.itemUseTime = logicMgr.LongJourneyHellManager.getItemUseTime();
        self.useMaxTime = dataTable.getGlobalInt(672);
        if self.itemUseTime < self.useMaxTime  then
            self.txtLongJourney2.text = "[color=#A0C776]" .. (self.useMaxTime - self.itemUseTime) .. "/" .. self.useMaxTime.. "[/color]";
        else
            self.txtLongJourney2.text = "[color=#FF2A4D]" .. (self.useMaxTime - self.itemUseTime) .. "/" .. self.useMaxTime.. "[/color]";
        end
    end
end

function t:ItemRender(index,go)
    local item = logicMgr.CtrlManager.GetCtrl(PanelNames.HeadRectItem)
    local data = self.heroDataList[index]
    item.initData(go,data)

    local hp = 100;
    local mp = 0;
    if self.openType == 1 then
        local heroInfo = logicMgr.ExpeditionMgr.GetHeroInfo(data.partnerId)
        if heroInfo then
            hp = heroInfo.hp;
            mp = heroInfo.energy;
        else
            mp = 100;
        end
    elseif self.openType == 2 then
        local heroInfo = logicMgr.LongJourneyHellManager.getMemberList(data.partnerId);
        if heroInfo then
            hp = heroInfo.hpRate;
            mp = heroInfo.mpRate;
        end
    end
    if hp == 0 then
        go.grayed = true
    else
        go.grayed = false
    end
    item.UpdateHPMPBar(go,hp,mp)
end
function t:ClickClose()
    closeUI(PanelResNames.UIExpeditionHolyStone)
end
function t:CanFix()
    local isCan = nil
    if self.openType == 1 then
        for _,v in pairs(self.heroDataList) do
            local config = logicMgr.ExpeditionMgr.GetHeroInfo(v.partnerId)
            if config and (config.hp < 100 or config.energy < 100)then
                isCan =true
                break
            end
        end
    elseif self.openType == 2 then
        for _,v in pairs(self.heroDataList) do
            local config = logicMgr.LongJourneyHellManager.getMemberList(v.partnerId);
            if config and config.hpRate < 100 then
                isCan =true
                break
            end
        end
    end
    return isCan
end
function t:ClickSure()
    local count = logicMgr.ItemManager.getItemCountbyModelId(EnumConst.ItemIDEnum.HolyStone)
    if count == 0 then
        UISysTips.AddMsg(getLanguage("expedition_stone_not_enough"))
        AudioManager.Play(2010)
        return
    end
    if self.openType == 2 and self.itemUseTime >= self.useMaxTime then
        UISysTips.AddMsg(getLanguage("LongJourneyUsageTips"))
        AudioManager.Play(2010)
        return
    end
    local isCan = self:CanFix()
    if not isCan then
        UISysTips.AddMsg(getLanguage("expedition_stone_use_fail_tip"))
        AudioManager.Play(2010)
        return
    end
    AudioManager.Play(2028)
    local itemData = {};
    itemData["itemId"] = logicMgr.ItemManager.getItemIdByModelId(EnumConst.ItemIDEnum.HolyStone)
    itemData["itemModelId"] = EnumConst.ItemIDEnum.HolyStone
    itemData["itemNum"] = 1;
    if self.openType == 1 then
        itemData["parameters"] = {};
    elseif self.openType == 2 then
        itemData["parameters"] = {2};
    end
    itemHandler.sendReqUseItem(itemData)
    UISysTips.AddMsg(getLanguage("expedition_stone_use_suc_tip"))
    self:ClickClose()
end
function t:ClickGet()
    --showUI(PanelNames.UISource, function ()
    --    self:ClickClose();
    --    if t.goFunc then
    --        t.goFunc();
    --    end
    --end, EnumConst.ItemIDEnum.HolyStone);
    self.showSourceController.selectedIndex = 1;
    self.showSourceList = {EnumConst.ItemIDEnum.HolyStone}
    self:updateSource();
end

function t:updateSource()
    logicMgr.ItemManager.updateSourceUI(self.sourceUIobj, self.showSourceList, function ()
        self.showSourceController.selectedIndex = 0;
    end, function ()
        self.showSourceController.selectedIndex = 0;
        self:ClickClose();
        if self.goFunc then
            self.goFunc();
        end
    end );
end


return t